<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<title>首页</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="" />

<!-- easyui -->
<script type="text/javascript" th:src="@{/easyui/jquery.min.js}" ></script>
<script type="text/javascript" th:src="@{/easyui/jquery.easyui.min.js}"></script>
<link rel="stylesheet" type="text/css" th:href="@{/easyui/themes/default/easyui.css}">
<link rel="stylesheet" type="text/css" th:href="@{/easyui/themes/icon.css}">
<link rel="stylesheet" type="text/css" th:href="@{/easyui/themes/color.css}">
	<script type="text/javascript" th:src="@{/easyui/locale/easyui-lang-zh_CN.js}"></script>
</head>
<body>
	<!-- 工具条 --> 
	<div th:replace="commons/pub::#list"></div>
	<div th:replace="commons/pub::#tool_bar"></div>
	<!-- 编辑窗口 -->
	<div id="win">
		<form id="editForm" method="post">
			设备编号：<input name="did" type="number" class="easyui-validatebox" data-options="required:true"/><br>
			名称：<input name="name" class="easyui-validatebox" data-options="required:true"/><br>
			规格：<input name="util" class="easyui-validatebox"/><br>
			属性:<input name="attribute" class="easyui-validatebox" /><br>
			购入金额:<input name="price" class="easyui-validatebox"/><br>
			购入日期:<input name="buy_date" class="easyui-validatebox"/><br>
			厂家:<input name="factory" class="easyui-validatebox"/><br>
			负责人:<input name="responsible" class="easyui-validatebox"/><br>
			联系电话:<input name="telephone" class="easyui-validatebox"/><br>
			状态:<input name="state" type="radio" class="easyui-validatebox" value="0"/>报废
			<input name="state" type="radio" class="easyui-validatebox"  value="1"/>正常
			<input name="state" type="radio" class="easyui-validatebox"  value="2"/>维修<br>
			使用情况:<input name="used" type="radio" class="easyui-validatebox" value="0"/>空闲
			<input name="used" type="radio" class="easyui-validatebox"  value="1"/>在使用中<br>
			<a id="saveBtn" href="#" class="easyui-linkbutton">保存</a>
		</form>
	</div>
	<script type="text/javascript" th:inline="none">
	$(function(){
		$('#list').datagrid({   
		    url:'/device/listByPage',
		    columns:[[   
		        {field:'id',checkbox:true},
		        {field:'did',title:'编号',width:'5%',align:'center'},
		        {field:'name',title:'名称',width:'12%',align:'center'},
		        {field:'util',title:'规格',width:'5%',align:'center'},
		        {field:'attribute',title:'属性',width:'10%',align:'center'},
				{field:'price',title:'购入金额',width:'10%',align:'center'},
				{field:'buy_date',title:'购入日期',width:'10%',align:'center'},
				{field:'factory',title:'厂家',width:'13%',align:'center'},
				{field:'responsible',title:'负责人',width:'5%',align:'center'},
				{field:'telephone',title:'联系电话',width:'10%',align:'center'},
				{field:'state',title:'状态',width:'5%',align:'center'},
				{field:'used',title:'使用情况',width:'8%',align:'center'}
		    ]],
		    pagination:true,
		    toolbar:"#tool_bar"
		}); 
		$('#win').window({
		    width:600,   
		    height:400, 
		    padding:30,
		    closed:true,
		    title:"用户编辑",
		    modal:true  
		});
		/*增加用户*/
		$("#addBtn").click(function(){
			$("#editForm").form("clear");
			$("#win").window("open");
		});
		/*保存*/
		$("#saveBtn").click(function () {
			$("#editForm").form("submit",{
				url:"/device/saveDevice",     //提交表单
				onSubmit:function(){
					return $("#editForm").form("validate");
				},
				success:function(data){   //回传数据
					data=eval("("+data+")")
					if(data.success){
						$("#win").window("close");
						$("#list").datagrid("reload");
						$.messager.alert("提示","保存成功!","info");
					}else{
						$.messager.alert("提示","保存失败：",data.msg,"error");
					}
				}
			});
		})
		//修改数据
		$("#editBtn").click(function(){
			//判断只能选择一行
			var rows=$("#list").datagrid("getSelections");
			if(rows.length!=1){
				$.messager.alert("提示","修改操作只能选择一行","warning");
				return;
			}
			//表单回显
			$("#editForm").form("load","/device/findDeviceById?did="+rows[0].did+"");
			$("#win").window("open");
		});
		//删除功能
		$("#deleteBtn").click(function(){
			//判断只能选择一行
			var rows=$("#list").datagrid("getSelections");
			if(rows.length==0){
				$.messager.alert("提示","删除操作至少选择一行","warning");
				return;
			}
			$.messager.confirm("提示","确定删除吗？",function(value){
				if(value){
					//执行删除,遍历数据
					var idStr= "";
					$(rows).each(function(i){
						idStr+=("did="+rows[i].did);
						if(i!=$(rows).length-1){
							idStr+="&";
						}
					});
					$.post("/device/delectDevice",idStr,function(data){
						if(data.success){
							$("#list").datagrid("reload");
							$.messager.alert("提示","删除成功!","info");
						}else{
							$.messager.alert("提示","删除失败：",data.msg,"error");
						}
					},"json");
				}
			});
		});
	});
	</script>

</body>